package com.qg.cyapp.activity;

import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.qg.cyapp.R;
import com.qg.cyapp.bean.Item;
import com.qg.cyapp.bean.Order;
import com.qg.cyapp.util.ShareUtil;

public class OrderDetailActivity extends HttpActivity {

	private TextView tvId, tvMealTimeName, tvSum, tvTime, tvReturn;
	private LinearLayout llFoodItems;
	private Order order;
	private List<Item> items;
	private LayoutInflater inflater;

	@Override
	public void afterPost(String result, int requestCode,
			Map<String, String> request) {
		// TODO Auto-generated method stub
		bean = getGsonBean(result);
		if (bean.state == ShareUtil.SUCCESS) {
			items = gson.fromJson(bean.json, new TypeToken<List<Item>>() {
			}.getType());
			for (Item item : items) {
				addFoodItem(item);
			}

		}
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_order_detail;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		int position = getIntent().getIntExtra(ShareUtil.POSITION_KEY, -1);
		order = WatchOrdersActivity.orders.get(position);
		inflater = LayoutInflater.from(this);

		llFoodItems = (LinearLayout) findViewById(R.id.llFoodItems);
		tvId = (TextView) findViewById(R.id.tvOrderId);
		tvMealTimeName = (TextView) findViewById(R.id.tvMealTimeName);
		tvTime = (TextView) findViewById(R.id.tvTime);
		tvSum = (TextView) findViewById(R.id.tvSumPrice);
		tvReturn = (TextView) mfindViewAndSetOnClick(R.id.tvReturn);

		tvId.setText(order.id);
		tvMealTimeName.setText(order.mealTimesName);
		tvTime.setText(order.time);
		tvSum.setText(String.valueOf(order.sum) + "元");

		getOrderDetail();
	}

	public void addFoodItem(Item item) {
		View view = inflater.inflate(R.layout.item_show_food_item, null);
		TextView tvName, tvPrice, tvCount, tvSum;
		tvName = (TextView) view.findViewById(R.id.tvName);
		tvPrice = (TextView) view.findViewById(R.id.tvPrice);
		tvSum = (TextView) view.findViewById(R.id.tvSum);
		tvCount = (TextView) view.findViewById(R.id.tvCount);

		tvName.setText(item.food.name);
		tvPrice.setText(String.valueOf(item.food.price) + "元");
		tvCount.setText(String.valueOf(item.number));
		tvSum.setText(String.valueOf(item.food.price * item.number) + "元");

		llFoodItems.addView(view);

	}

	private void getOrderDetail() {
		params.clear();
		params.put("orderMenuId", order.id);
		params.put("action", "viewOrderDetails");
		addPostRequest("OrderAction", params, 0);
		sendPostRequest();
	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.tvReturn:
			finish();
			break;
		}
	}

}
